package Easy;

import java.util.HashMap;
import java.util.Map;

public class Solution141 {

    class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
            next = null;
        }
    }

    public boolean hasCycle(ListNode head) {
        Map<ListNode,Integer> map = new HashMap<ListNode,Integer>();
        ListNode p = head;
        //int ans = -1;
        int count = 0;
        while(p!=null){
            if(map.containsKey(p)){
                //ans = map.get(p);
                return true;
            }else{
                map.put(p,count);
                count++;
                p = p.next;
            }
        }

        return false;

    }
}
